package cn.actoncode.boot.framework.common.http.apache;

import cn.actoncode.boot.framework.common.exception.ServiceErrorException;
import cn.actoncode.boot.framework.common.http.RequestHttp;
import cn.actoncode.boot.framework.common.http.RoutineUEPostRequestExecutor;
import cn.actoncode.boot.framework.common.http.enums.PlatformType;
import cn.hutool.core.collection.CollUtil;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.collect.Lists;
import org.apache.http.Consts;
import org.apache.http.HttpHost;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicNameValuePair;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * .
 *
 * @author bin
 * @date 2017/5/4
 */
public class ApacheRoutineUEPostRequestExecutor extends RoutineUEPostRequestExecutor<CloseableHttpClient, HttpHost> {
    public ApacheRoutineUEPostRequestExecutor(RequestHttp requestHttp) {
        super(requestHttp);
    }

    @Override
    public ObjectNode execute(String uri, Map<String, String> headers, Map<String, String> data, PlatformType platformType) throws ServiceErrorException, IOException {
        HttpPost httpPost = new HttpPost(uri);
        if (requestHttp.getRequestHttpProxy() != null) {
            RequestConfig config = RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build();
            httpPost.setConfig(config);
        }

        if (CollUtil.isNotEmpty(headers)) {
            for (final Map.Entry<String, String> entry : headers.entrySet()) {
                httpPost.addHeader(entry.getKey(), entry.getValue());
            }
        }

        if (CollUtil.isNotEmpty(data)) {
            List<NameValuePair> nameValuePairList = buildNameValuePairList(data);
            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(nameValuePairList, Consts.UTF_8);
            httpPost.setEntity(entity);
        }

        try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost)) {
            String responseContent;
            if (platformType == PlatformType.XFYUN) {
                responseContent = XfyunUtf8ResponseHandler.INSTANCE.handleResponse(response);
            } else {
                responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
            }
            return this.handleResponse(platformType, responseContent);
        } finally {
            httpPost.releaseConnection();
        }
    }

    private List<NameValuePair> buildNameValuePairList(Map<String, String> data) {
        List<NameValuePair> list = Lists.newArrayList();
        data.forEach((key, value) -> {
            NameValuePair nameValuePair = new BasicNameValuePair(key, value + "");
            list.add(nameValuePair);
        });
        return list;
    }
}
